package xlstask

import (
	"bytes"
	"errors"
	"fmt"
	"gitee.com/guoliben/extrame/xls"
	"strings"
)

func ProcessFile(path string) ([]byte, error) {
	file, err := xls.Open(path, "utf-8")
	if err != nil || file == nil {
		fmt.Println("Error opening xls file:", err)
		return nil, err
	}
	var b bytes.Buffer
	for _, v := range file.ReadAllCells(999999) {
		joined := strings.Join(v, "")
		b.WriteString(joined)
	}
	return b.Bytes(), nil
}

func ProcessBuffer(data []byte) ([]byte, error) {
	if len(data) > 0 {
		file, err := xls.OpenReader(bytes.NewReader(data), "utf-8")
		if err != nil || file == nil {
			fmt.Println("Error creating xls reader:", err)
			return nil, err
		}
		var b bytes.Buffer
		for _, v := range file.ReadAllCells(999999) {
			joined := strings.Join(v, "")
			b.WriteString(joined)
		}
		return b.Bytes(), nil
	}
	return nil, errors.New("No data")
}
